package com.myhdg.school;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.myhdg.school.fragments.BaseFragment;
import com.myhdg.school.fragments.CampusFragment;
import com.myhdg.school.fragments.CurriculumFragment;
import com.myhdg.school.fragments.FindFragment;
import com.myhdg.school.fragments.FragmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ViewPager contentPagers;
    TabLayout bottomBar;

    List<BaseFragment> tabsFragment;

    DrawerLayout drawerLayout;
    NavigationView navigationView;

    ImageView personal;
    TextView currentPage;
    ImageView option;

    ImageView headImage;
    EditText mottoEdit;

    SharedPreferences configure;
    SharedPreferences.Editor editor;

    long firstTime = 0;//首次点击返回键时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        configure = getPreferences(MODE_PRIVATE);
        editor = configure.edit();

        contentPagers = findViewById(R.id.contentPagers);
        bottomBar = findViewById(R.id.bottomBar);

        //=====侧滑栏=====
        drawerLayout = findViewById(R.id.drawerLayout);
        navigationView = findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
//                drawerLayout.closeDrawer(navigationView);//关闭侧滑栏
                return true;
            }
        });
        View headView = navigationView.getHeaderView(0);
        headImage = headView.findViewById(R.id.headImage);
        mottoEdit = headView.findViewById(R.id.motto);
        mottoEdit.setText(configure.getString("Motto"
                , "一个人至少拥有一个梦想，有一个理由去坚强。心若没有栖息的地方，到哪里都是在流浪。"));
        mottoEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                editor.putString("Motto", mottoEdit.getText().toString());
                editor.apply();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        currentPage = findViewById(R.id.currentPager);
        personal = findViewById(R.id.personal);
        personal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(navigationView);
            }
        });
        option = findViewById(R.id.option);

        initPagers();
    }

    private void initPagers() {
        tabsFragment = new ArrayList<>();
        bottomBar.removeAllTabs();
        bottomBar.setTabMode(TabLayout.MODE_FIXED);
        bottomBar.setSelectedTabIndicatorHeight(0);//设置线条宽度
        ViewCompat.setElevation(bottomBar, 10);//设置按下效果

        tabsFragment.add(new CurriculumFragment());
        tabsFragment.add(new CampusFragment());
        tabsFragment.add(new FindFragment());
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), tabsFragment, MainActivity.this);
        contentPagers.setAdapter(fragmentAdapter);
        contentPagers.setOffscreenPageLimit(2);//设置每页预加载页数
        bottomBar.setupWithViewPager(contentPagers);
        contentPagers.setCurrentItem(0);//设置当前页
        for (int i = 0; i < bottomBar.getTabCount(); i++) {
            TabLayout.Tab itemTab = bottomBar.getTabAt(i);
            if (itemTab != null) {
                View view = fragmentAdapter.getView(i);
                if (i == 0) {
                    view.setSelected(true);
                }
                itemTab.setCustomView(view);
            }
        }
        final List<String> tabsId = new ArrayList<>();
        tabsId.add("课程表");
        tabsId.add("校园");
        tabsId.add("发现");
        bottomBar.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPage.setText(tabsId.get(tab.getPosition()));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (drawerLayout.isDrawerOpen(navigationView)) {
                drawerLayout.closeDrawer(navigationView);
                return true;
            } else {
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    Snackbar.make(MainActivity.this.findViewById(android.R.id.content), "再按一次将退出School ！", Snackbar.LENGTH_SHORT).show();
                    firstTime = secondTime;
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
